package com.example.study.common.login.controllers;

import com.example.study.common.base.RestResponse;
import com.example.study.common.login.dto.LoginDTO;
import com.example.study.common.login.dto.RegisterDTO;
import com.example.study.common.login.dto.UserTokenWrapperDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author kuotian
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    @PostMapping("/login")
    public RestResponse login(@RequestBody LoginDTO loginDTO) {
        UserTokenWrapperDTO userTokenWrapperDTO = new UserTokenWrapperDTO();
        return RestResponse.build(userTokenWrapperDTO);
    }

    @PostMapping("/register")
    public RestResponse register(@RequestBody RegisterDTO registerDTO) {
        String token = makeToken();
        return RestResponse.build(UserTokenWrapperDTO.builder()
                .token(token)
                .hasPassword(true)
                .build());
    }

    private String makeToken() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
}
